package dao.Impl;

import dao.UserDao;
import entity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.C3p0Helper;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> findAll() {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from user";
        try {
            return qr.query(sql, new BeanListHandler<>(User.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<User> findUserByName(String userName) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from user where userName=?";
        try {
            return (List<User>) qr.query(sql, new BeanListHandler<>(User.class), userName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User findUserById(int id) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from user where id=?";
        try {
            return (User) qr.query(sql, new BeanListHandler<>(User.class),id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void addUser(User user) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "insert into user(userName,password,sex,age,telephone,email,address,pic,state) values(?,?,?,?,?,?,?,?,?)";
        try {
            qr.update(sql,user.getUserName(),user.getPassword(),user.getSex(),user.getAge(),user.getTelephone(),user.getEmail(),user.getAddress(),user.getPic(),user.getState());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

//    @Override
//    public void updateUser(User user) {
//        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
//        String sql = "update user set userName=?,password=?,sex=?,age=?,telephone=?,email=?,address=?,pic=?,state=? where id=?";
//        try {
//            qr.update(sql,user.getUserName(),user.getPassword(),user.getSex(),user.getAge(),user.getTelephone(),user.getEmail(),user.getAddress(),user.getPic(),user.getState());
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//
//    }
@Override
public void updateUser(User user) {
    QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
    String sql = "update user set userName=?,password=?,sex=?,age=?,telephone=?,email=?,address=?,pic=?,state=? where id=?";
    try {
        // 确保这里传递了10个参数，最后一个参数是user.getId()
        qr.update(sql,
                user.getUserName(),
                user.getPassword(),
                user.getSex(),
                user.getAge(),
                user.getTelephone(),
                user.getEmail(),
                user.getAddress(),
                user.getPic(),
                user.getState(),
                user.getId() // 这是第10个参数，确保它存在
        );
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

    @Override
    public void deleteUser(int id) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "delete from user where id=?";
        try {
            qr.update(sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User login(String username, String password) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "select * from user where userName=? and password=?";
        ResultSetHandler<User> setHandler = new BeanHandler<>(User.class);
        try {
            return qr.query(sql,setHandler,username,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
